cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _isCapturing:false,
    },

    // use this for initialization
    onLoad: function () {
    },
    
    init:function(){
        this.ANDROID_API = "com/ypgames/wzmj/WXAPI";
        this.IOS_API = "AppController";
    },
    
    login:function(){
        if(cc.sys.os == cc.sys.OS_ANDROID){ 
            console.log("cc.sys.OS_ANDROID");
            jsb.reflection.callStaticMethod(this.ANDROID_API, "Login", "()V");
        }
        else if(cc.sys.os == cc.sys.OS_IOS){
            jsb.reflection.callStaticMethod(this.IOS_API, "login");
        }
        else{
            console.log("platform:" + cc.sys.os + " dosn't implement share.");
        }
    },
    
    share:function(title,desc,type){
        if(cc.sys.os == cc.sys.OS_ANDROID){
            jsb.reflection.callStaticMethod(this.ANDROID_API, "Share", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Z)V",cc.vv.SI.appweb,title,desc,type);
        }
        else if(cc.sys.os == cc.sys.OS_IOS){
            jsb.reflection.callStaticMethod(this.IOS_API, "share:shareTitle:shareDesc:type:",cc.vv.SI.appweb,title,desc,type);
        }
        else{
            console.log("platform:" + cc.sys.os + " dosn't implement share.");
        }
    },
    
    shareResult:function(type){
        if(this._isCapturing){
            return;
        }
        this._isCapturing = true;
        var size = cc.director.getWinSize();
        var currentDate = new Date();
        var fileName = "result_share.jpg";
        var fullPath = jsb.fileUtils.getWritablePath() + fileName;
        if(jsb.fileUtils.isFileExist(fullPath)){
            jsb.fileUtils.removeFile(fullPath);
        }
        var texture = new cc.RenderTexture(Math.floor(size.width), Math.floor(size.height));
        texture.setPosition(cc.p(size.width/2, size.height/2));
        texture.begin();
        cc.director.getRunningScene().visit();
        texture.end();
        texture.saveToFile(fileName, cc.IMAGE_FORMAT_JPG);
        
        var self = this;
        var tryTimes = 0;
        var fn = function(){
            if(jsb.fileUtils.isFileExist(fullPath)){
                var height = 100;
                var scale = height/size.height;
			    var width = Math.floor(size.width * scale);
                
                if(cc.sys.os == cc.sys.OS_ANDROID){
                    jsb.reflection.callStaticMethod(self.ANDROID_API, "ShareIMG", "(Ljava/lang/String;IIZ)V",fullPath,width,height,type);
                }
                else if(cc.sys.os == cc.sys.OS_IOS){
                    jsb.reflection.callStaticMethod(self.IOS_API, "shareIMG:width:height:type:",fullPath,width,height,type);
                }
                else{
                    console.log("platform:" + cc.sys.os + " dosn't implement share.");
                }
                self._isCapturing = false;
            }
            else{
                tryTimes++;
                if(tryTimes > 10){
                    console.log("time out...");
                    return;
                }
                setTimeout(fn,50); 
            }
        }
        setTimeout(fn,50);
    },
    
    onLoginResp:function(code){
        cc.log("login wechat resp");
        cc.log(code);
        var fn = function(ret){
            // console.log("ret date");
            // console.log(ret);
            if(ret.errcode == 0){
                cc.sys.localStorage.removeItem("wx_account");
                cc.sys.localStorage.removeItem("wx_sign");
                
                cc.sys.localStorage.setItem("wx_account",ret.account);
                cc.sys.localStorage.setItem("wx_sign",ret.sign);
                //额外要添加的信息
                cc.vv.userMgr.login_id=ret.userid;
                // console.log("cc.vv.userMgr.login_id",cc.vv.userMgr.login_id);
                if(cc.vv.baseInfoMap == null){
                    cc.vv.baseInfoMap = {};
                 }
                //  console.log("headimgurl of"+ret.headimgurl);
                 if(ret.headimgurl){
                        var url = ret.headimgurl + ".jpg";
                }
                cc.vv.userMgr.img_url=url;
                // console.log("------mg_url="+cc.vv.userMgr.img_url);
                // console.log("---------------------------------------"+url);
                // console.log("name="+ret.name+" sex="+ret.sex+" url="+url);
                 var info = {
                    name:ret.name,
                    sex:ret.sex,
                    url:url,
                }
                // cc.vv.baseInfoMap[ret.userid] =info;

            }
            cc.vv.userMgr.onAuth(ret);
        }
        if(cc.vv.LoginHandler){
            // console.log("emit login_checkout_push-----------------------");
            cc.vv.LoginHandler.emit("login_checkout_push");
        }
        cc.vv.http.sendRequest("/wechat_auth",{code:code,os:cc.sys.os},fn);

    },
    onShareResp:function(){
        var account = cc.sys.localStorage.getItem("wx_account");
        var userId = cc.vv.userMgr.userId;
        console.log("account"+account);
        console.log("userId="+userId);
        var fn = function(ret)
        {
            if(ret.errcode == 0)
            {
                cc.vv.alert.show("提示","分享成功");
                var fs = function(ret)
                {
                    console.log("ret.errcode="+ret.errcode);
                    if(ret.errcode==0){
                        console.log("share succuse ret="+ret);
                        cc.vv.userMgr.gems = ret.data.gems;
                        console.log(ret);
                        console.log("cc.vv.userMgr.gems"+cc.vv.userMgr.gems);
                        if(cc.vv.hallHandle){
                            cc.vv.hallHandle.emit("refreshGemTip_push");
                        }
                    }else{
                        console.log("ret.errcode!=0 in onShareResp");
                    }
                }
                cc.vv.http.sendRequest("/getGems",{account:account},fs);
            }
            else{
                // cc.vv.alert.show("提示","分享失败");
            }

            if(cc.vv.hallHandle){
                cc.vv.hallHandle.emit("close_share_win_push");
            }
        }
        
        cc.vv.http.sendRequest("/share",{userId:userId},fn);
    }
});
